package com.czxy.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.czxy.tanhua.dubbo.mappers.UserInfoMapper;
import com.czxy.tanhua.entity.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/4/3 9:03
 */
@DubboService
public class UserInfoApilmpl implements UserInfoApi {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Override
    public void adduserInfo(UserInfo userInfo) {
        System.out.println(userInfo);
        int insert = userInfoMapper.insert(userInfo);
        System.out.println(insert);
    }

    @Override
    public void update(UserInfo userInfo) {
        int i = userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findUser(Long id) {
        UserInfo userInfo = this.userInfoMapper.selectById(id);
        return userInfo;
    }

    @Override
    public void editUser(UserInfo userInfo, Long id) {
        userInfo.setId(id);
        this.userInfoMapper.updateById(userInfo);
    }

    @Override
    public Map<Long, UserInfo> findByids(List<Long> ids, UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.in(UserInfo::getId,ids);
        userInfoLambdaQueryWrapper.le(StringUtils.isNotEmpty(userInfo.getAge().toString()),UserInfo::getAge,userInfo.getAge());
        userInfoLambdaQueryWrapper.eq(StringUtils.isNotEmpty(userInfo.getGender()),UserInfo::getGender,userInfo.getGender());
        //查询
        List<UserInfo> userInfoList = userInfoMapper.selectList(userInfoLambdaQueryWrapper);
        //处理数据Map.Key
        Map<Long, UserInfo> map = CollUtil.fieldValueMap(userInfoList, "id");
        return map;
    }

    @Override
    public Map<Long, UserInfo> findUserByids(List<Long> collect) {
        Map<Long,UserInfo> userInfoMap = new HashMap<>();
        //传入的是用户id
        for (Long userid : collect) {
            UserInfo userInfo = userInfoMapper.selectById(userid);
            if (userInfo!=null){
                userInfoMap.put(userInfo.getId(),userInfo);
            }
        }
        return userInfoMap;
    }
}
